<template>
  <div>
    <el-form :model="socEventForm" inline :rules="rules" ref="soEventFormRef" label-width="100px" style="width: 100%">
      <el-form-item label="封面" prop="cover">
        <el-upload
            class="avatar-uploader"
            :action="'/api/file/upload/event/cover'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
        >
          <img v-if="socEventForm.cover" alt="回显失败" :src="socEventForm.cover" class="avatar"/>
          <el-icon v-else class="avatar-uploader-icon">
            <Plus/>
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="组织者ID" prop="organizerId">
        <el-input v-model.number="socEventForm.organizerId"></el-input>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="socEventForm.title"></el-input>
      </el-form-item>
      <el-form-item label="活动地点" prop="location">
        <el-input v-model="socEventForm.location"></el-input>
      </el-form-item>
      <el-form-item label="参与条件" prop="condition">
        <el-input></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
            v-model="socEventForm.startTime"
            type="datetime"
            placeholder="选择开始时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
            v-model="socEventForm.endTime"
            type="datetime"
            placeholder="选择结束时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="socEventForm.description"></el-input>
      </el-form-item>
      <el-form-item label="是否审核" prop="requiresApproval">
        <el-radio-group v-model="socEventForm.requiresApproval">
          <el-radio :value="0">不需要</el-radio>
          <el-radio :value="1">需要</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="socEventForm.status">
          <el-radio :value="0">启用</el-radio>
          <el-radio :value="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="活动风采" prop="miens">
        <el-image
            v-for="(item,index) in socEventForm.miens"
            :key="index"
            :src="item.url"
            :preview-src-list="socEventForm.miens.map(item=>item.url)"
            style="width: 100px;height: 100px;margin: 10px"
        >
        </el-image>
        <el-button :icon="Plus" @click="mienDialogVisible=true">添加</el-button>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <TinymceEdider v-model="socEventForm.content"
                       scene-code="user:event"></TinymceEdider>
      </el-form-item>
    </el-form>
    <el-button style="width: 100%" type="primary" @click="submitSocEvent(soEventFormRef)">提交</el-button>
    <el-dialog
        v-model="centerDialogVisible"
        width="400"
        align-center
    >
      <el-result
          icon="success"
          title="提交成功！"
          sub-title="快去活动列表中查看吧"
      >
        <template #extra>
          <el-button type="primary" @click="centerDialogVisible = false">
            确定
          </el-button>
        </template>
      </el-result>
    </el-dialog>
    <el-dialog
        v-model="mienDialogVisible"
        title="添加风采"
        width="500"
        align-center
    >
      <el-form>
        <el-form-item label="描述">
          <el-input v-model="eventMienForm.description" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
              action="/api/file/upload/event/mien"
              :on-success="handleMienUpFile"
          >
            <el-button type="primary"> 点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="mienDialogVisible = false">
            取消
          </el-button>
          <el-button type="primary" @click="submitEventMien">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {ref, reactive, onMounted, watch} from "vue";
import {useRouter, useRoute} from "vue-router";
import {Plus} from "@element-plus/icons-vue";
import type {FormInstance, FormRules, UploadProps} from "element-plus";
import type {SocEvents, SocEventCreateDTO, SocEventMien} from "@/api/social/event/type";
import {reqAddEvent, reqGetEventById, reqUpdateEvent, reqGetEventMienList} from "@/api/social/event";
import TinymceEdider from "@/components/common/TinymceEdider.vue";

let loading = ref<boolean>(true);
let socEventForm = reactive<SocEventCreateDTO>({
  title: '',
  requiresApproval: 0,
  description: '',
  content: '',
  cover: '',
  location: '',
  startTime: null,
  endTime: null,
  status: 0,
  miens: [],//风采
});
let centerDialogVisible = ref(false)
let mienDialogVisible = ref(false)
let route = useRoute();
const rules = reactive<FormRules<SocEvents>>({
  organizerId: [
    {required: true, message: '请输入组织者ID', trigger: 'blur'},
    {type: 'number', message: '组织者ID必须是数字', trigger: 'blur'},
  ],
  title: [
    {required: true, message: '请输入标题', trigger: 'blur'},
  ],
  requiresApproval: [
    {required: true, message: '请选择是否需要审核', trigger: 'change'},
  ],
  cover: [
    {required: true, message: '请上传宣传封面', trigger: 'blur'},
  ],
  description: [
    {required: true, message: '请输入描述', trigger: 'blur'},
  ],
  content: [
    {required: true, message: '请输入活动内容', trigger: 'blur'},
  ],
  location: [
    {required: true, message: '请输入活动地点', trigger: 'blur'},
  ],
  status: [
    {required: true, message: '请输入状态', trigger: 'blur'},
    {type: 'number', message: '状态必须是数字', trigger: 'blur'},
  ],
  startTime: [
    {required: true, message: '请选择开始时间', trigger: 'change'},
  ],
  endTime: [
    {required: true, message: '请选择结束时间', trigger: 'change'},
  ],
})
const router = useRouter();
let soEventFormRef = ref();
let isAdd = ref(true);
let eventMienForm = reactive({
  url: '',
  description: '',
})

const handleSetHtml = (textContent: string, htmlContent: string) => {

};
const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
  // imageUrl.value = URL.createObjectURL(uploadFile.raw!)
  socEventForm.cover = response.data
}
let handleMienUpFile = (res) => {
  console.log("上传回调", res.data)
  eventMienForm.url = res.data
}
let submitEventMien = () => {
  socEventForm.miens.push({...eventMienForm})
  eventMienForm.url = ''
  eventMienForm.description = ''
  mienDialogVisible.value = false
}
let submitSocEvent = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid) => {
    if (valid) {
      if (isAdd.value) {
        await reqAddEvent(socEventForm);
        centerDialogVisible.value = true;
      } else {
        await reqUpdateEvent(socEventForm);
        centerDialogVisible.value = true;
      }
    } else console.log("error submit!",);
  });
};


onMounted(() => {
  if (route.query.eventId != null) {
    reqGetEventById(route.query.eventId as string).then((res) => {
      Object.assign(socEventForm, res.data)
      isAdd.value = false;
    })
    reqGetEventMienList(route.query.eventId as string).then(res => {
      socEventForm.miens = res.data
    })
  }
})

</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
